package com.fangcun.controller.sys;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.tencent.TencentUtils;
import com.fangcun.entity.SysArea;
import com.fangcun.service.SystemService;

/**
 * @作者： 陳曉鬆 @时间： 2020年4月29日 - 下午3:00:13 @description：->
 */
@RestController
@RequestMapping("/sys/{version}")
public class AreaController {

	private static Logger LOG = LoggerFactory.getLogger(AreaController.class);

	@Resource
	private SystemService systemService;

	@Autowired
	private TencentUtils tencentUtils;

	/** 城市级联数据 */
	@ApiVersion(1.0)
	@RequestMapping("/area/list")
	public ResponseMsg list(@RequestBody SysArea request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(systemService.searchAreaList(request));
		} catch (ProException e) {
			LOG.error("-系统-城市级联数据出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-系统-城市级联数据异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** JSSDK CONFIG */
	@ApiVersion(1.0)
	@RequestMapping("wx/jssdk/config")
	public ResponseMsg jssdkConfig(@RequestBody Map<String, String> param) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			String appId = param.get("appId");
			String url = param.get("url");
			if (url.contains("#")) {
				url = url.split("#")[0];
			}
			url = URLDecoder.decode(url.trim(), "UTF-8");
			Map<String, String> reData = new HashMap<>();
			reData.put("nonceStr", PublicMethod.getCode(16));
			reData.put("timeStamp", (new java.util.Date().getTime() / 1000) + "");
			reData.put("url", url);
			reData.put("jsapi_ticket", tencentUtils.getJsSdkTicketToken(appId));

			String sb = "jsapi_ticket=" + reData.get("jsapi_ticket") + "&noncestr=" + reData.get("nonceStr") + "&timestamp=" + reData.get("timeStamp") + "&url="
					+ reData.get("url");
			// LOG.info("-微信-jssdk加密-原参数->{}", sb);
			reData.put("signature", DigestUtils.sha1Hex(sb));
			// LOG.info("-微信-jssdk加密-加密参数->{}", reData.get("signature"));
			reData.put("appId", appId);
			reData.remove("url");
			reData.remove("jsapi_ticket");
			msg.setData(reData);
		} catch (ProException e) {
			LOG.error("-系统-jssdk出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-系统-jssdk异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
